/*
 * @Description: 权限
 * @Author: pinus
 * @Email: pinus0716@163.com
 * @Git: https://gitee.com/xyf97
 * @Date: 2021-10-08 15:22:52
 * @LastEditTime: 2021-10-08 15:33:19
 */
const express = require('express')
const router = express.Router()
const path = require('path')

// 获取验证模块
const authorization = require(path.join(process.cwd(), '/modules/authorization'))

// 通过验证模块构建权限服务模块
const rightService = authorization.getService('RightService')

// 获取所有权限列表
router.get(
  '/:type',
  // 参数验证
  function (req, res, next) {
    if (!req.params.type) {
      return res.sendResult(null, 400, '显示类型未定义')
    }
    if (req.params.type != 'list' && req.params.type != 'tree') {
      return res.sendResult(null, 400, '显示类型参数错误')
    }
    next()
  },
  // 业务逻辑
  function (req, res, next) {
    rightService.getAllRights(req.params.type, function (err, rights) {
      if (err) return res.sendResult(null, 400, err)
      res.sendResult(rights, 200, '获取权限列表成功')
    })(req, res, next)
  }
)

module.exports = router
